package cn.com.huzo.webapp.controller.tech;

import cn.com.huzo.model.entities.tech.CostForecastHeaderEntity;
import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.tech.CostForecastLinesEntity;
import cn.com.huzo.model.entities.tech.CostForecastLinesPictureEntity;
import cn.com.huzo.service.tech.CostForecastHeaderManager;
import cn.com.huzo.service.tech.CostForecastLinesManager;
import cn.com.huzo.service.tech.CostForecastLinesPictureManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 成本预测控制器
 *
 * @author 吴旭
 */
@Controller
public class CostForecastController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    @Resource(name = "CostForecastHeaderManager")
    private CostForecastHeaderManager _manager_header;

    @Resource(name = "CostForecastLinesManager")
    private CostForecastLinesManager _manager_lines;

    @Resource(name = "CostForecastLinesPictureManager")
    private CostForecastLinesPictureManager _manager_lines_pic;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到成本预测一览画面
     * <p>
     * @return
     */
    @RequestMapping("tech/cost_forecast/list.html")
    public ModelAndView redirectListWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("tech.cost_forecast.list.def");
        return view;
    }

    /**
     * 跳转成本预测编辑画面
     * <p>
     * @param request 上下文请求
     * @param action 操作类别 1：增加 2：编辑
     * @param headerId 成本预测ID
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("tech/cost_forecast/{action}/{headerId}/edit.html")
    public ModelAndView redirectEditWnd(HttpServletRequest request, @PathVariable(value = "action") int action,
            @PathVariable(value = "headerId") Long headerId) throws Exception {
        ModelAndView view = new ModelAndView();
        // 为画面设置变量
        view.addObject("action", action);
        view.addObject("headerId", headerId);
        String strImageUrl = "http://" + request.getServerName() // 服务器地址  
                + ":"
                + request.getServerPort() // 端口号  
                + "/erpimg";
        view.addObject("strImageUrl", strImageUrl);    // 设置图片显示路径
        view.setViewName("tech.cost_forecast.edit.def");
        return view;
    }

    /**
     * 弹出成本预测选择画面
     * <p>
     * @return
     */
    @RequestMapping("tech/cost_forecast/choice.html")
    public ModelAndView redirectPieceYardagePopupWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.tech.cost_forecast.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="主表-数据处理部分">
    /**
     * 得到新的单据号
     * <p>
     * @return @throws Exception
     */
    @RequestMapping(value = "/tech/cost_forecast/header/fetchNewBillNo.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchNewBillNo() throws Exception {
        Map<String, Object> _result = new HashMap();
        _result.put("BillNo", _manager_header.fetchNewBillNo());
        return _result;
    }

    /**
     * 根据标识取得成本预测数据。
     *
     * @param criterion 成本预测标识、当前用户、流程标识
     * @return 成本预测数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/cost_forecast/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchByHeaderKey(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "CurrentUserId"),
                MapUtils.getLong(criterion, "WorkFlowId")
        );
    }

    /**
     * 删除成本预测信息数据。
     *
     * @param criterion 要删除的数据
     * @return 删除后的数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/cost_forecast/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.delete(criterion);
    }

    /**
     * 根据条件取得成本预测信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 成本预测信息分页数据
     * @throws Exception
     */
    @RequestMapping(value = "tech/cost_forecast/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchForPage(criterion);

    }

    /**
     * 新增成本预测信息数据。
     * <p>
     * @param instance 要保存的成本预测信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "tech/cost_forecast/header/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> add(@RequestBody final CostForecastHeaderEntity instance) throws Exception {
        return _manager_header.add(instance);

    }

    /**
     * 更新成本预测信息数据。
     * <p>
     * @param instance 要保存的成本预测信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "tech/cost_forecast/header/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer update(@RequestBody final CostForecastHeaderEntity instance) throws Exception {
        return _manager_header.update(instance);

    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="明细-数据处理部分">
    /**
     * 根据主表标识，取得明细表数据。
     *
     * @param headerId 成本预测ID
     * @return 明细列表数据
     * @throws Exception
     */
    @RequestMapping(value = "tech/cost_forecast/fetchLinesByHeaderId.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<CostForecastLinesEntity> fetchLinesByHeaderKey(@RequestBody final Long headerId) throws Exception {
        return _manager_lines.fetchLinesByHeaderKey(headerId);
    }

    /**
     * 增加明细数据(选择材料)。
     *
     * @param itemList 要增加的材料
     * @return 增加后的成本预测明细
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/cost_forecast/lines/insert.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<CostForecastLinesEntity> insertLinesByNewMaterial(@RequestBody final List<CostForecastLinesEntity> itemList) throws Exception {
        return _manager_lines.insertLinesByNewMaterial(itemList);

    }

    /**
     * 更新明细数据。
     *
     * @param itemList 要保存的数据
     * @return 更新后的成本预测明细
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/cost_forecast/lines/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateLines(@RequestBody final List<CostForecastLinesEntity> itemList) throws Exception {
        if (itemList != null && !itemList.isEmpty()) {
            return _manager_lines.batchUpdate(itemList);

        } else {
            return new HashMap();
        }
    }

    /**
     * 根据料号更该明细材料数据。
     *
     * @param criterion 要更新的数据（行标识、料号）
     * @return 更新后的成本预测明细。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/cost_forecast/lines/updateLinesByMaterialId.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateLinesByMaterialId(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.updateLinesByMaterialId(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getString(criterion, "LinesId"),
                MapUtils.getLong(criterion, "MaterialId"),
                MapUtils.getLong(criterion, "UpdatedBy")
        );
    }

    /**
     * 删除明细数据。
     *
     * @param criterion 要删除的明细标识、主表id、删除的明细信息等
     * @return 删除后的数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/cost_forecast/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.delete(criterion);
    }

    /**
     * 批量删除明细数据。
     *
     * @param itemList 成本预测明细标识
     * @return 删除后的数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/cost_forecast/lines/batch_delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<CostForecastLinesEntity> batchDeleteLines(@RequestBody final List<CostForecastLinesEntity> itemList) throws Exception {
        return _manager_lines.batchDeleteLines(itemList);
    }

    /**
     * 引入类似款号的成本预测明细。
     *
     * @param criterion 要引入的成本预测
     * @return 引入后的成本预测明细
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/cost_forecast/lines/importLinesFromOther.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> importLinesFromOther(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_lines.importLinesFromOtherCostForecast(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "ImportedHeaderId"),
                MapUtils.getLong(criterion, "CreatedBy"),
                MapUtils.getLong(criterion, "UpdatedBy"));
    }

    /**
     * 从剪切板粘贴数据。
     *
     * @param criterion 剪切板数据
     * @return 粘贴后的成本预测明细。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/cost_forecast/lines/paste.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<CostForecastLinesEntity> insertFromClipboard(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.insertFromClipboard(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getString(criterion, "CopiedLinesId"),
                MapUtils.getLong(criterion, "MaterialId"),
                MapUtils.getLong(criterion, "UomId"),
                MapUtils.getString(criterion, "Specification"),
                MapUtils.getInteger(criterion, "OrderNo"),
                MapUtils.getLong(criterion, "CreatedBy"),
                MapUtils.getLong(criterion, "UpdatedBy"));
    }

    /**
     * 更新成本预测明细排列顺序。
     *
     * @param itemList 要更改顺序的成本预测明细
     * @return 影响行数
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/tech/cost_forecast/lines/updateLinesOrder.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateLinesOrder(@RequestBody final List<CostForecastLinesEntity> itemList) throws Exception {
        return _manager_lines.updateLinesOrder(itemList);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="图片处理部分">
    /**
     * 保存成本预测图片数据。
     * <p>
     * @param instance 要保存的成本预测图片对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "/tech/cost_forecast/picture/save.html",
            method = RequestMethod.POST,
            produces = {"application/json"})
    public @ResponseBody
    Map<String, Object> savePicture(@RequestBody final CostForecastLinesPictureEntity instance) throws Exception {
        Map<String, Object> _result = new HashMap();
        _result.put("PictureId", _manager_lines_pic.insert(instance));

        return _result;
    }

    /**
     * 删除成本预测图片数据。
     *
     * @param instance 成本预测图片对象
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/tech/cost_forecast/picture/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer deletePicture(@RequestBody final CostForecastLinesPictureEntity instance) throws Exception {
        return _manager_lines_pic.delete(instance);
    }
    // </editor-fold>
}
